<template>
    <div class="container containerStudent">
        <!-- header -->
        <div class="header">
            <div class="header-box">
                <p class="header-title">学校名称:</p>
                <!-- <el-cascader
                    v-model="header.school"
                    :options="header.schoolList"
                    @change="getTableData(1)"
                    size="small"
                    clearable
                    :props="{
                        value: 'value',
                        label: 'label',
                        children: 'children'
                    }"
                /> -->
                <el-cascader
                    placeholder="请选择"
                    :options="provideList"
                    filterable
                    clearable
                    change-on-select
                    expand-trigger="click"
                    v-model="header.school"
                    @change="getTableData(1)"
                    size="small"
                    :props="{
                        label: 'name',
                        value: 'id',
                        children: 'children'
                    }"
                />
            </div>
            <div class="header-box">
                <p class="header-title">用户:</p>
                <el-input
                    v-model="header.tel"
                    placeholder="用户ID或手机号"
                    suffix-icon="el-icon-search"
                    size="small"
                    clearable
                    class="search-ipt"
                    @change="getTableData(1)"
                    @keyup.native.enter="getTableData(1)"
                />
            </div>
            <div class="header-box">
                <p class="header-title">邀请码:</p>
                <el-input
                    v-model="header.invitation"
                    placeholder="请输入邀请码"
                    suffix-icon="el-icon-search"
                    size="small"
                    clearable
                    class="search-ipt"
                    @change="getTableData(1)"
                    @keyup.native.enter="getTableData(1)"
                />
            </div>
            <div class="header-box">
                <p class="header-title">考核结果:</p>
                <el-select
                    v-model="header.result"
                    placeholder="请选择"
                    size="small"
                    @change="getTableData(1)"
                    clearable
                >
                    <el-option
                        v-for="item in header.resultList"
                        :key="item.value"
                        :label="item.label"
                        :value="item.value"
                    />
                </el-select>
            </div>
            <div class="header-box">
                <p class="header-title">代理类目:</p>
                <el-select
                    v-model="header.type"
                    filterable
                    placeholder="请选择"
                    size="small"
                    clearable
                    @change="getTableData(1)"
                >
                    <el-option
                        v-for="item in leavelType"
                        :key="item.cate_no"
                        :label="item.cate_name"
                        :value="item.cate_no"/>
                </el-select>
            </div>
            <div class="header-box">
                <p class="header-title">代理店铺:</p>
                <el-input
                    v-model="header.agent_shop"
                    placeholder="请输入代理店铺名称"
                    suffix-icon="el-icon-search"
                    size="small"
                    clearable
                    class="search-ipt"
                    @change="getTableData(1)"
                    @keyup.native.enter="getTableData(1)"
                />
            </div>
            <div class="header-box">
                <p class="header-title">等级:</p>
                <el-select
                    v-model="header.leavel"
                    filterable
                    placeholder="请选择"
                    size="small"
                    clearable
                    @change="getTableData(1)"
                >
                    <el-option
                        v-for="item in leavelData"
                        :key="item.value"
                        :label="item.label"
                        :value="item.value"/>
                </el-select>
            </div>
            <!-- <div class="header-box">
                <p class="header-title">选择时间:</p>
                <el-date-picker
                    v-model="date"
                    type="datetimerange"
                    :picker-options="pickerOptions()"
                    range-separator="至"
                    start-placeholder="开始日期"
                    end-placeholder="结束日期"
                    :default-time="['00:00:00', '23:59:59']"
                    align="right"
                    size="small"
                    @change="getTableData(1)"
                />
            </div> -->
            <div class="header-box">
                <el-button
                    type="primary"
                    size="small"
                    @click="fnFormReset()"
                >
                    重置
                </el-button>
            </div>
            <!-- <div class="header-box">
                <el-button
                    type="primary"
                    size="small"
                    @click="fnExportTable()"
                    :disabled="!authResult.auth_page"
                >
                    导出数据
                </el-button>
            </div> -->
        </div>
        <!-- /header -->

        <!-- table -->
        <el-table
            :data="tableData"
            height="100%"
            style="width: 100%;"
            @sort-change="handleSortChanged"
            border
        >
            <el-table-column
                label="序号"
                width="100px"
                align="center"
                type="index"
                fixed="left"
            />
            <el-table-column
                prop="invite_code"
                label="邀请码"
                fixed="left"
                align="center"
                show-overflow-tooltip
                :formatter="emptyFormatter"
            />
            <el-table-column
                prop="true_name"
                label="掌大精英姓名"
                :width="140"
                align="center"
                show-overflow-tooltip
                :formatter="emptyFormatter"
            />
            <el-table-column
                prop="student_no"
                label="用户ID"
                :width="160"
                align="center"
                show-overflow-tooltip
                :formatter="emptyFormatter"
            />
            <!-- <el-table-column
                prop="join_time"
                label="加入时间"
                :width="140"
                align="center"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                sortable="custom"
            >
                <template slot-scope="scope">
                    {{ scope.row.join_time * 1000 | dateFormatter('yyyy/MM/dd HH:mm') }}
                </template>
            </el-table-column> -->
            <el-table-column
                prop="agent_level"
                label="等级"
                align="center"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                sortable="custom"
            >
                <template slot-scope="scope">
                    E{{ scope.row.agent_level }}
                </template>
            </el-table-column>
            <el-table-column
                prop="agent_shop_num"
                label="代理店铺"
                align="center"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                :width="100"
            >
                <template slot-scope="scope">
                    <span
                        class="curse-add"
                        @click="fnCheckAgent(scope.row)"
                        v-if="Number(scope.row.agent_shop_num) > 0"
                    >
                        {{ scope.row.agent_shop_num }}
                    </span>
                    <span v-else>
                        --
                    </span>
                </template>
            </el-table-column>
            <el-table-column
                prop="phone"
                label="手机号"
                :width="120"
                align="center"
                show-overflow-tooltip
                :formatter="emptyFormatter"
            />
            <el-table-column
                prop="school_name"
                label="学校名称"
                align="center"
                show-overflow-tooltip
                :formatter="emptyFormatter"
            />
            <el-table-column
                prop="fans_buy_num"
                label="零元购粉丝人数"
                align="center"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                :width="150"
                sortable="custom"
            >
                <template slot-scope="scope">
                    <div
                        class="curse-add"
                        style="text-align:center;">
                        {{ scope.row.fans_buy_num }}
                    </div>
                </template>
            </el-table-column>
            <!-- 1.3.5版本移除 -->
            <!-- <el-table-column
                prop="post_accept_num"
                label="分享采纳次数"
                align="center"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                :width="120"
            >
                <template slot-scope="scope">
                    <div
                        @click="fnGoPage(scope.row)"
                        class="curse-add"
                        style="text-align:center;">
                        {{ scope.row.post_accept_num }}
                    </div>
                </template>
            </el-table-column> -->
            <!-- <el-table-column
                label="EI得分"
                align="center"
                show-overflow-tooltip
                sortable="custom"
                :formatter="emptyFormatter"
                :width="90"
            >
                <template slot-scope="scope">
                    <div style="text-align:center;">
                        {{ scope.row.agent_score }}
                    </div>
                </template>
            </el-table-column> -->
            <!-- <el-table-column
                label="查看个人答题"
                align="center"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                width="120"
            >
                <template slot-scope="scope">
                    <span
                        class="checkNum"
                        @click="fnOpenDia(scope.row)"
                    >查看</span>
                </template>
            </el-table-column> -->
            <!-- <el-table-column
                label="粉丝答题"
                align="center"
                show-overflow-tooltip
                :formatter="emptyFormatter"
            >
                <template slot-scope="scope">
                    <span
                        class="checkNum"
                        @click="fnCheckNum(scope.row)"
                    >查看</span>
                </template>
            </el-table-column> -->
            <el-table-column
                prop="fans_num"
                label="直属粉丝"
                align="center"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                sortable="custom"
                :width="100"
            >
                <template slot-scope="scope">
                    <span
                        class="curse-add"
                        @click="fnGoFans(scope.row, '1')"
                    >{{ scope.row.fans_num }}</span>
                </template>
            </el-table-column>
            <el-table-column
                prop="grand_fans_num"
                label="二级粉丝"
                :width="100"
                align="center"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                sortable="custom"
            />
            <el-table-column
                prop="goods_agent_cate_name"
                label="代理类目"
                align="center"
                show-overflow-tooltip
                :formatter="emptyFormatter"
            />
            <el-table-column
                prop="buy_num"
                label="独立购买人数"
                align="center"
                :width="130"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                sortable="custom"
            >
                <template slot-scope="scope">
                    <span
                        class="curse-add"
                        @click="fnGoFans(scope.row, '2')"
                    >{{ scope.row.buy_num }}</span>
                </template>
            </el-table-column>
            <el-table-column
                prop="order_num"
                label="总订单数"
                align="center"
                :width="100"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                sortable="custom"
            />
            <el-table-column
                prop="self_order_num"
                label="自营订单数"
                align="center"
                :width="120"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                sortable="custom"
            />
            <el-table-column
                prop="consume_money"
                label="有效消费金额"
                :width="130"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                align="center"
                sortable="custom"
            >
                <template slot-scope="scope">
                    <span>{{ scope.row.consume_money | setMoney }}元</span>
                </template>
            </el-table-column>
            <el-table-column
                prop="rebate_money"
                label="所得分佣"
                :width="100"
                align="center"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                sortable="custom"
            >
                <template slot-scope="scope">
                    <span>{{ scope.row.rebate_money | setMoney }}元</span>
                </template>
            </el-table-column>
            <el-table-column
                prop="status"
                label="考核结果"
                align="center"
                show-overflow-tooltip
                :formatter="emptyFormatter"
            >
                <template slot-scope="scope">
                    {{ scope.row.appraisal | setAppraisa }}
                </template>
            </el-table-column>
            <el-table-column
                prop="agent_type"
                label="精英类型"
                show-overflow-tooltip
                :formatter="emptyFormatter"
            >
                <template slot-scope="scope">
                    <span v-if="scope.row.agent_type == 1">自由精英</span>
                    <span v-else-if="scope.row.agent_type == 2">专职精英</span>
                    <span v-else>--</span>
                </template>
            </el-table-column>
            <el-table-column
                label="操作"
                fixed="right"
                align="center"
                :width="160"
            >
                <template slot-scope="scope">
                    <el-button
                        type="text"
                        class="curse-add"
                        @click="fnUpgrade(scope.row, 1)"
                    >
                        升级
                    </el-button>
                    <el-button
                        type="text"
                        class="curse-add"
                        @click="fnUpgrade(scope.row, 2)"
                    >
                        降级
                    </el-button>
                    <!-- 按钮权限配置 -->
                    <el-button
                        type="text"
                        class="curse-add"
                        @click="fnUpgrade(scope.row, 3)"
                    >
                        淘汰
                    </el-button>
                    <!-- <el-button
                        type="text"
                        :class=" scope.row.appraisal == -1 || scope.row.appraisal == 0 ? 'curse-add' : 'curse-add-disabled' "
                        :disabled="scope.row.appraisal == 1"
                        @click="fnQualified(scope.row)"
                    >
                        合格
                    </el-button>
                    <el-button
                        type="text"
                        :class=" scope.row.appraisal == 1 || scope.row.appraisal == 0 ? 'curse-add' : 'curse-add-disabled' "
                        :disabled="scope.row.appraisal == -1"
                        @click="fnUnqualified(scope.row)"
                    >
                        不合格
                    </el-button> -->
                </template>
            </el-table-column>
        </el-table>
        <!-- /table -->

        <!-- 分页 -->
        <div
            class="pagination-wrapper clearfix"
        >
            <el-pagination
                background
                layout="total, prev, pager, next"
                :current-page.sync="page.now_page"
                :total="page.total_count"
                :page-size="page.page_size"
                @current-change="getTableData"
            />
        </div>
        <!-- /分页 -->

        <!-- 粉丝答题弹窗 -->
        <el-dialog
            title="答题人数"
            :visible.sync="dialogShow"
            :before-close="handleCancle"
            width="50%"
        >
            <el-table
                :data="answerNum"
                style="width: 100%">
                <el-table-column
                    align="center"
                    type="index"
                    label="序号"
                />
                <el-table-column
                    align="center"
                    prop="custom_form_title"
                    label="问卷标题"
                />
                <el-table-column
                    align="center"
                    prop="number"
                    label="答题人数">
                    <template slot-scope="scope">
                        {{ scope.row.submit_count ? scope.row.submit_count : '0' }}
                    </template>
                </el-table-column>
            </el-table>
        </el-dialog>
        <!-- /答题人数弹窗 -->

        <!-- 个人答题弹窗 -->
        <el-dialog
            title="精英答题弹窗"
            :visible.sync="dialogVisiable"
            :before-close="handleCancleAnswer"
            width="50%"
        >
            <el-table
                :data="personalInfo"
                style="width: 100%">
                <el-table-column
                    type="index"
                    label="序号"
                    align="center"
                />
                <el-table-column
                    prop="custom_form_title"
                    label="问卷标题"
                    align="center"
                />
                <el-table-column
                    align="center"
                    label="操作">
                    <template slot-scope="scope">
                        <div
                            class="checkNum"
                            @click="fnGoCheckAnswer(scope.row)">查看答题</div>
                    </template>
                </el-table-column>
            </el-table>
        </el-dialog>
        <!-- /答题人数弹窗 -->

        <!-- 代理店铺 -->
        <el-dialog
            :visible.sync="dialogAgentShop"
            :close-on-click-modal="false"
            width="500px"
        >
            <el-table
                :data="agentShopList"
                style="width: 100%"
            >
                <!-- 测试字段 -->
                <el-table-column
                    prop="shop_title"
                    align="center"
                    label="店铺名称"
                />
                <el-table-column
                    prop="verify_time"
                    align="center"
                    label="代理时间"
                >
                    <template slot-scope="scope">
                        {{ scope.row.verify_time * 1000 | dateFormatter('yyyy/MM/dd HH:mm') }}
                    </template>
                </el-table-column>
            </el-table>
            <div class="zd-dialog-foot">
                <el-button
                    size="small"
                    type="primary"
                    @click="dialogAgentShop = false"
                >
                    确定
                </el-button>
            </div>
        </el-dialog>
        <!-- /代理店铺 -->

        <!-- 升级弹窗 -->
        <el-dialog
            :visible.sync="dialogUpgrad"
            :close-on-click-modal="false"
            width="360px"
            center
        >
            <img
                src="@/assets/image/zd-jg.png"
                class="zd-jg"
            >
            <p class="zd-jg-title">是否升级</p>
            <p style="width:100%;text-align: center">
                一旦确认，将用户等级将会立即升级，请谨慎操作
            </p>
            <div class="zd-foot">
                <el-button
                    size="small"
                    @click=" dialogUpgrad = false "
                    style="width:100px"
                >
                    取消
                </el-button>
                <el-button
                    type="primary"
                    size="small"
                    style="margin-left: 40px; width:100px"
                    @click="fnSetUpgrand()"
                >
                    确定
                </el-button>
            </div>
        </el-dialog>

        <!-- 降级弹窗 -->
        <el-dialog
            :visible.sync="dialogDowngrad"
            :close-on-click-modal="false"
            width="360px"
            center
        >
            <img
                src="@/assets/image/zd-jg.png"
                class="zd-jg"
            >
            <p class="zd-jg-title">是否降级</p>
            <p style="width:100%;text-align: center">
                降级最低只能降到该用户本来的等级
            </p>
            <div class="zd-foot">
                <el-button
                    size="small"
                    @click=" dialogDowngrad = false "
                    style="width:100px"
                >
                    取消
                </el-button>
                <el-button
                    type="primary"
                    size="small"
                    style="margin-left: 40px;width:100px"
                    @click="fnSetDowngrand()"
                >
                    确定
                </el-button>
            </div>
        </el-dialog>

        <!-- 淘汰弹窗 -->
        <el-dialog
            :visible.sync="dialogTao"
            :close-on-click-modal="false"
            width="622px"
            title="淘汰该用户"
        >
            <p class="zd-tips">一旦淘汰，将会取消该用户精英的身份，且不可取消，请非常谨慎的操作！</p>
            <div class="zd-content">
                <p class="zd-content-title">淘汰理由:</p>
                <el-input
                    type="textarea"
                    :autosize="{ minRows: 4, maxRows: 6}"
                    placeholder="请填写淘汰理由"
                    v-model="tao_text"
                />
            </div>
            <div
                class="zd-foot"
                style="margin-bottom:0"
            >
                <el-button
                    size="small"
                    @click=" dialogTao = false "
                    style="width:100px"
                >
                    取消
                </el-button>
                <el-button
                    type="primary"
                    size="small"
                    style="margin-left: 40px;width:100px"
                    @click="fnConfirmtao()"
                    :disabled="!tao_text"
                >
                    确定
                </el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
import { setDate } from '@/assets/js/utils';
import {getDate} from '@/assets/js/utils';
export default {
    /**
     * @param  {Array}     tableData        //表单数据源
     * @param  {Object}    page             //分页
     * @param  {String}    school           //学校名称
     * @param  {Array}     schoolList       //城市+学校数据
     * @param  {String}    tel              //手机号码
     * @param  {Array}     date             //选择时间
     * @param  {String}    invitation       //邀请码
     * @param  {Number}    result           //考核结果
     * @param  {Array}     resultList       //考核结果数据
     * @param  {Number}    prevPage         //掌大精英管理页码
     * @param  {Object}    prevHead         //掌大精英筛选数据
     */
    data () {
        return {
            tao_text: '',
            dialogTao: false,
            dialogDowngrad: false,
            agent_level: '',
            agent_no: '',
            dialogUpgrad: false,
            dialogAgentShop: false,
            agentShopList: [],
            leavelData: [{
                value: '',
                label: '全部'
            },{
                value: 0,
                label: 'E0'
            },{
                value: 1,
                label: 'E1'
            },{
                value: 2,
                label: 'E2'
            },{
                value: 3,
                label: 'E3'
            },{
                value: 4,
                label: 'E4'
            },{
                value: 5,
                label: 'E5'
            }],
            leavelType: [],
            provideList: [],
            date: [],
            header: {
                type: '',
                prevHead: {},
                prevPage: '',
                date: [],
                tel: '',
                schoolList: [],
                school: '',
                invitation: '',
                result: 99,
                agent_shop: '',
                leavel: '',
                resultList: [{
                    value: 99,
                    label: '全部'
                },{
                    value: 0,
                    label: '未判断'
                },{
                    value: 1,
                    label: '合格'
                },{
                    value: -1,
                    label: '不合格'
                }]
            },
            dialogVisiable: false,
            page: {
                now_page: 1,
                total_count: 0,
                page_size: 0
            },
            tableData: [],
            authResult: {},
            dialogShow: false,
            answerNum:[],
            personalInfo:[],
            sortMethod:'desc',
            sortFlag: 1,
            filterData: {
                sort_field: 0,
                sort_type: 0
            }
        };
    },
    filters: {
        setMoney (val) {
            if (val === null) {
                return '0.00';
            }
            return Number(val).toFixed(2);
        },
        setAppraisa (val) {
            if (val == 1) {
                return '合格';
            } else if (val == -1) {
                return '不合格';
            } else if (val == 0) {
                return '--';
            }
        }
    },
    watch: {
        dialogTao (val) {
            if(!val) {
                this.tao_text = '';
            }
        }
    },
    created () {
        this.authResult = this.$route.meta.authResult;
        this.init();

        console.log(this.authResult, 'authResult');
    },
    mounted () {
        if (window.history && window.history.pushState && this.prevPage !== '') {
            history.pushState(null, null, document.URL);
            window.addEventListener('popstate', this.goBack, false);
        }
    },
    destroyed(){
        if (this.prevPage !== '') {
            window.removeEventListener('popstate', this.goBack, false);
        }
    },
    methods: {
        /**
         * 设置表单header
         */
        setHeader () {
            this.$store.commit('SET_HEADER', [{
                title: "APP后台",
            },
            {
                title: '掌大精英'
            },
            {
                title: "掌大精英数据"
            }]
            );
        },

        //表单数据初始化
        init() {
            this.getParam();
            this.setHeader();
            this.getTableData();
            // this.getSchoolName();
            this.getSchoolTree();
            this.getCateList();
        },

        /**
         * 获取url参数
         */
        getParam () {
            let query = this.$route.query;

            if (query.phone !== undefined || query.page !== undefined || query.header !== undefined ) {
                this.header.tel = query.phone;
                this.prevPage = query.page;
                this.prevHead = query.header;
            }

            if(query.cate_no) {
                this.header.type = query.cate_no;
            }

            if(query.shop_title) {
                this.header.agent_shop = query.shop_title;
            }
        },

        /**
         * 监听返回事件
         */
        goBack () {
            if (this.prevPage !== '') {
                this.$router.replace({
                    name: 'studentAdministration',
                    query: {
                        page: this.prevPage,
                        header: this.prevHead
                    }
                });
            }
        },

        /**
         * 获取省+市+学校三级联动数据
         */
        getSchoolTree () {
            this.$post('/student/local_info%5Cschool_tree', {},resp=>{
                if (resp.code == 1) {
                    this.provideList = resp.data.tree;
                } else if (resp.code == 0) {
                    this.$message({
                        type: 'waring',
                        message: resp.msg
                    });
                }
            });
        },

        /**
         * 获取代理类目商品列表
         */
        getCateList () {
            this.$post('/student/stuagent_rebate%5Cget_goods_agent_cate_simple_list', {},resp=>{
                if (resp.code == 1) {
                    this.leavelType = resp.data.list;
                } else if (resp.code == 0) {
                    this.$message({
                        type: 'waring',
                        message: resp.msg
                    });
                }
            });
        },

        /**
         * 获取学校+城市
         */
        getSchoolName () {
            this.$post('/student/region_college%5Ccity_college_linkage', {},resp=>{
                if (resp.code == 1 && resp.data.length > 0) {
                    let list = [];

                    resp.data.map((item,index)=>{
                        list.push({
                            value: item.id,
                            label: item.name,
                            children: []
                        });
                        item.children.map(itm=>{
                            list[index].children.push({
                                value: itm.id,
                                label: itm.name
                            });
                        });
                        return false;
                    });
                    this.header.schoolList = list;
                } else if (resp.code == 0) {
                    this.$message({
                        type: 'waring',
                        message: resp.msg
                    });
                }
            });
        },

        /**
         * 获取表格数据
         */
        getTableData (page) {
            this.$post('/student/stuagent_rebate%5Cget_rebate_list', {
                page_id: page || this.page.now_page,
                // school_id: this.header.school !== '' ? this.header.school[1] : '',
                province_id: this.header.school !== '' ? this.header.school[0] : '',
                city_id: this.header.school !== '' ? this.header.school[1] : '',
                school_id: this.header.school !== '' ? this.header.school[2] : '',
                keyword_student_id: this.header.tel,
                invite_code: this.header.invitation,
                appraisal: this.header.result,
                // start_time: this.date === null ? '' : (this.date.length == 0 ? '' : getDate(new Date(this.date[0]))),
                // end_time: this.date === null ? '' : (this.date.length == 0 ? '' : getDate(new Date(this.date[1]))),
                sort_field: this.filterData.sort_field,
                sort_type: this.filterData.sort_type,
                goods_agent_cate_no: this.header.type,
                agent_level: this.header.leavel,
                shop_keyword: this.header.agent_shop
            }, resp => {
                if (resp.code == 1) {
                    this.tableData = resp.data.list;
                    this.page.now_page = resp.data.page.now_page;
                    this.page.total_count = resp.data.page.total_count;
                    this.page.page_size = resp.data.page.page_size;
                }
            });
        },

        /**
         * 打开弹窗
         */
        fnUpgrade (row, status) {
            this.agent_no = row.agent_no;
            this.agent_level = row.agent_level;

            if(status == 1) {
                if(Number(this.agent_level) > 5) {
                    this.$message({
                        type: 'warning',
                        message: '抱歉,已经是最高等级了'
                    });
                    return;
                }
                this.dialogUpgrad = true;
            } else if (status == 2) {
                if(Number(this.agent_level) == 0) {
                    this.$message({
                        type: 'warning',
                        message: '抱歉,已经是最低等级了'
                    });
                    return;
                }
                this.dialogDowngrad = true;
            } else if (status == 3) {
                if(this.authResult.auth_tao) {
                    this.dialogTao = true;
                } else {
                    this.$message({
                        type: 'warning',
                        message: '抱歉,你还没有权限'
                    });
                    return;
                }

            }
        },

        /**
         * 确定升级
         */
        fnSetUpgrand () {
            this.$post('/student/stuagent_rebate%5Cupgrade_level',{
                agent_no: this.agent_no
            },resp=> {
                if (resp.code == 1) {
                    this.dialogUpgrad = false;
                    this.$message({
                        type: 'success',
                        message: `已将该精英升级至E${Number(this.agent_level + 1)}`
                    });
                    this.getTableData(1);
                } else if (resp.code == 0) {
                    this.$message({
                        type: 'warning',
                        message: resp.msg
                    });
                }
            });
        },

        /**
         * 确认降级
         */
        fnSetDowngrand () {
            this.$post('/student/stuagent_rebate%5Cdegrade_level',{
                agent_no: this.agent_no
            },resp=> {
                if (resp.code == 1) {
                    this.dialogDowngrad = false;
                    this.$message({
                        type: 'success',
                        message: `已将该精英降级至E${Number(this.agent_level - 1)}`
                    });
                    this.getTableData(1);
                } else if (resp.code == 0) {
                    this.$message({
                        type: 'warning',
                        message: resp.msg
                    });
                }
            });
        },

        /**
         * 淘汰
         */
        fnConfirmtao (row) {
            this.$post('/student/stuagent_rebate%5Crevoke_agent',{
                agent_no: this.agent_no,
                reason: this.tao_text
            },resp=> {
                if (resp.code == 1) {
                    this.dialogTao = false;
                    this.$message({
                        type: 'success',
                        message: '已取消该用户的精英身份',
                        center: true
                    });
                    this.getTableData(1);
                } else if (resp.code == 0) {
                    this.$message({
                        type: 'warning',
                        message: resp.msg
                    });
                }
            });
        },

        /**
         * 查看代理店铺
         */
        fnCheckAgent (row) {
            this.$post('/student/shop_agent_shop_student%5Cget_agent_shop_list',{
                agent_no: row.agent_no
            },resp=> {
                if (resp.code == 1) {
                    this.agentShopList = resp.data.list;
                    this.dialogAgentShop = !this.dialogAgentShop;
                } else if (resp.code == 0) {
                    this.$message({
                        type: 'warning',
                        message: resp.msg
                    });
                }
            });
        },

        /**
         * 数据排序
         */
        handleSortChanged(data) {
            console.log(data, 'data');
            let sortTypeMap = {
                ascending: 0,
                descending: 1
            };

            // 0:加入时间,1:0元购人数,2:下属粉丝数,3:二级粉丝数,4:独立购买人数,5:总订单数,6:自营订单数,7:有效消费金额数,8:所得分佣金额,9:代理等级
            let sortFieldMap = {
                "fans_buy_num": 1,
                "fans_num": 2,
                "grand_fans_num": 3,
                "buy_num": 4,
                "order_num": 5,
                "self_order_num": 6,
                "consume_money": 7,
                "rebate_money": 8,
                "agent_level": 9
            };

            if (data.order) {
                this.filterData.sort_field = sortFieldMap[data.prop];
                this.filterData.sort_type = sortTypeMap[data.order];
            } else {
                this.filterData.sort_field = '';
                this.filterData.sort_type = '';
            }

            this.getTableData();
        },
        /**
         * 重置
         */
        fnFormReset () {
            this.header.result = 99;
            this.header.agent_shop = this.header.leavel = this.header.type = this.header.school = this.header.tel = this.header.invitation = '';
            this.getTableData();
        },

        /**
         * 导出数据
         */
        fnExportTable () {
            let base = this.$baseUrl;
            let ticket = localStorage.getItem('sessionTicket');

            let school = this.header.school !== '' ? this.header.school[1] : '';
            let invitation = this.header.invitation,
                tel = this.header.tel == undefined || this.header.tel == 'undefined' ? '' : this.header.tel ,
                result = this.header.result,
                start_date = this.header.date.length !== 0 ? this.header.date[0] : '',
                end_date = this.header.date.length !== 0 ? this.header.date[1] : '';

            console.log(school,'school', invitation, 'invitation', tel, 'tel', result, 'result', start_date, 'start_date', end_date, 'end_date', '11');

            let url = `${base}/student/stuagent_rebate%5Cexport_list?school_id=${school}&invite_code=${invitation}&phone=${tel}&appraisal=${result}&start_time=${start_date}&end_time=${end_date}&__ticket=${ticket}`;

            window.open(url);
        },

        /**
         * 合格
         */
        fnQualified (row) {
            this.$confirm('是否确定该管培生合格？', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                this.$post('/student/stuagent%5Cset_appraisal',{
                    student_no: row.student_no,
                    appraisal: 1
                },resp=> {
                    if (resp.code == 1) {
                        this.$set(row,'appraisal',1);
                        this.$message({
                            type: 'success',
                            message: '操作成功'
                        });
                    } else if (resp.code == 0) {
                        this.$message({
                            type: 'warning',
                            message: resp.msg
                        });
                    }
                });
            });
        },

        /**
         * 不合格
         */
        fnUnqualified (row) {
            this.$confirm('是否确定该管培生不合格？', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                this.$post('/student/stuagent%5Cset_appraisal',{
                    student_no: row.student_no,
                    appraisal: -1
                },resp=> {
                    if (resp.code == 1) {
                        this.getTableData();
                        this.$message({
                            type: 'success',
                            message: '操作成功'
                        });
                    } else if (resp.code == 0) {
                        this.$message({
                            type: 'warning',
                            message: resp.msg
                        });
                    }
                });
            });
        },

        /**
         * 进入管培生粉丝
         */
        fnGoFans (row, status) {
            let query = {
                phone: row.phone,
                true_name: row.true_name,
                agent_no: row.agent_no
            };

            if (status == 2) {
                query.purchase = 1;
            }

            this.$router.replace({
                name: 'studentPeiFans',
                query
            });

        },

        /**
         * 过滤函数
         */
        emptyFormatter(row, column, val) {
            if (!val) {
                return '--';
            }
            return val;
        },

        /**
         * 打开查看答题人数弹窗
         */
        fnCheckNum(row){
            this.dialogShow = true;
            // 获取答题人数的数据
            this.$post('/student/custom_form_data%5Cget_data_count_by_scene',{
                scene: 'stuagent_fans',
                scene_key:row.agent_no,
            },resp=> {
                if(resp.code == 1){
                    this.answerNum = resp.data.list;
                }
            });
        },

        /**
         * 关闭弹窗
         */
        handleCancle(){
            this.dialogShow = false;
        },
        /**
         * 跳转掌大精英动态
         */
        fnGoPage(row){
            this.$router.replace({
                name: 'eliteDynamics',
                query: {
                    phone: row.phone,
                    status: '1',
                }
            });
        },
        /**
         * 打开精英答题弹窗
         */
        fnOpenDia(row){
            this.personalInfo.sort(this.sortIndex);
            this.dialogVisiable = true;
            this.$post('/student/custom_form_data%5Cget_data_list_by_scene',{
                scene: 'stuagent',
                user_no:row.student_no,
            },resp=> {
                if(resp.code == 1){
                    this.personalInfo = resp.data.list;
                }
            });
        },
        /**
         * 关闭精英答题弹窗
         */
        handleCancleAnswer(){
            this.dialogVisiable = false;
        },

        /**
         * 查看答题
         */
        fnGoCheckAnswer(row){
            this.$router.replace({
                name: 'formPreview',
                query: {
                    id: row.data_id,
                    type: '2',
                    form_no:'',
                }
            });
        },

        /**
         * @description 时间处理
         */
        pickerOptions () {
            return {
                shortcuts : [{
                    text: '今天',
                    onClick(picker) {
                        let tempDate = new Date();
                        let retult_milliseconds = tempDate.getTime() + 1000 * 60 * 60 * 24 * 0;

                        tempDate.setTime(retult_milliseconds);

                        let result_Year = tempDate.getFullYear(),
                            result_Month = (tempDate.getMonth() + 1).toString(),
                            result_Date = tempDate.getDate().toString();

                        if (result_Month.length === 1) {
                            result_Month = "0" + result_Month;
                        }

                        if (result_Date.length === 1) {
                            result_Date = "0" + result_Date;
                        }

                        const start = result_Year + "-" + result_Month + "-" + result_Date + " " + "00:00:00";
                        const end = new Date();

                        picker.$emit('pick', [start, end]);
                    }
                },{
                    text: '昨天',
                    onClick(picker) {
                        const end = new Date();
                        const start = new Date();

                        start.setTime(start.getTime() - 3600 * 1000 * 24 * 1);
                        picker.$emit('pick', [start, end]);
                    }
                },{
                    text: '最近一周',
                    onClick(picker) {
                        const end = new Date();
                        const start = new Date();

                        start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
                        picker.$emit('pick', [start, end]);
                    }
                }, {
                    text: '最近一个月',
                    onClick(picker) {
                        const end = new Date();
                        const start = new Date();

                        start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
                        picker.$emit('pick', [start, end]);
                    }
                }, {
                    text: '最近三个月',
                    onClick(picker) {
                        const end = new Date();
                        const start = new Date();

                        start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
                        picker.$emit('pick', [start, end]);
                    }
                }, {
                    text: '最近半年',
                    onClick(picker) {
                        const end = new Date();
                        const start = new Date();

                        start.setTime(start.getTime() - 3600 * 1000 * 24 * 180);
                        picker.$emit('pick', [start, end]);
                    }
                }]
            };
        }
    }
};
</script>

<style lang="less" scoped>
    .container {
        background-color: #fff;
        padding: 15px;
        display: flex;
        flex-direction: column;
        position: relative;
        width: 100%;
        .header {
            position: relative;
            display: flex;
            flex-wrap: wrap;
            align-items: center;
            .header-box {
                display: flex;
                margin-right: 20px;
                align-items: center;
                color: #606266;
                margin-bottom: 20px;
                .header-title {
                    margin-right: 10px;
                    white-space: nowrap;
                }
                .btn-import-wrap {
                    margin-left: 15px;
                    display: inline-block;
                    position: relative;
                    overflow: hidden;
                    cursor: pointer;
                    vertical-align: top;
                    .import-input {
                        margin-left: -100%;
                        position: absolute;
                        left: 0;
                        top: 0;
                        width: 200%;
                        height: 100%;
                        cursor: pointer;
                        opacity: 0;
                    }
                }
            }
            .header-right {
                display: flex;
                .el-button {
                    margin-left: 20px;
                }
            }
        }
        .curse-add {
            color: rgb(64, 158, 255);
            cursor: pointer;
        }
        .checkNum{
            color: rgb(64, 158, 255);
            cursor:pointer;
        }
        .curse-add-disabled {
            color: #606266;;
            cursor: pointer;
            opacity: .7;
        }
        .pagination-wrapper {
            padding-top: 10px;
            .el-pagination {
                float: right;
                padding: 0;
                background-color: transparent;
            }
        }
        .dialog-footer {
            display: flex;
            justify-content: center;
            align-items: center;
        }
    }
    .el-dialogAdd {
        transition: all 1s;
    }
    .el-tag {
        margin: 0 15px 15px 0;
    }
    .zd-dialog-foot {
        width: 100%;
        display: flex;
        justify-content: center;
        align-items: center;
        margin-top: 20px;
    }
    .zd-foot {
        width: 100%;
        display: flex;
        align-items: center;
        justify-content: center;
        margin-top: 30px;
        margin-bottom: 25px;
    }
    .zd-jg {
        width: 48px;
        height: 48px;
        margin-left: 50%;
        transform: translateX(-50%);
    }
    .zd-jg-title {
        width: 100%;
        text-align: center;
        margin: 12px 0 20px 0;
        color: #333333;
        font-size: 16px;
    }
    .zd-tips {
        color: #F8A127;
        font-size: 14px;
        margin-bottom: 24px;
    }
    .zd-content {
        display: flex;
        .zd-content-title {
            color: #000;
            font-size: 14px;
            white-space: nowrap;
            margin-right: 6px;
        }
    }
</style>
<style lang="less">
    .containerStudent .el-table__body-wrapper .el-table__body .el-table__row td {
        border-right: none;
    }
    .containerStudent .el-table__header-wrapper .el-table__header .has-gutter tr th {
        border-right: none;
    }
    .containerStudent .el-table .el-table_1_column_6  .cell {
        position: relative;
    }
    .containerStudent .el-range-editor--small .el-range-separator{
        padding: 0 1px !important;
    }
</style>
